{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pickle\n",
    "import operator\n",
    "import pandas as pd\n",
    "import jieba\n",
    "from language.langconv import *"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Building prefix dict from the default dictionary ...\n",
      "Loading model from cache E:\\Temp\\jieba.cache\n",
      "Loading model cost 0.928 seconds.\n",
      "Prefix dict has been built succesfully.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20403\n"
     ]
    }
   ],
   "source": [
    "def Traditional2Simplified(sentence):\n",
    "    sentence = Converter('zh-hans').convert(sentence)\n",
    "    return sentence\n",
    "with open('cmn.txt', 'r', encoding='utf-8') as f:\n",
    "    lines = f.read().split('\\n');\n",
    "source_tokens=[]\n",
    "target_tokens=[]\n",
    "for pos, line in enumerate(lines):\n",
    "    line = line.split('\\t')\n",
    "    e = line[0][:-1] + \" \" + line[0][-1:]\n",
    "    c = line[1]\n",
    "    target_tokens.append(' '.join(e.split(' ')))\n",
    "    source_tokens.append(' '.join(jieba.lcut(Traditional2Simplified(c).strip(), cut_all=False)))\n",
    "#     source_tokens.append(' '.join(Traditional2Simplified(c).strip()))\n",
    "\n",
    "\n",
    "# 生成不同语言的词典\n",
    "def build_token_dict(token_list):\n",
    "    token_dict = {\n",
    "        '<PAD>': 0,\n",
    "        '<START>': 1,\n",
    "        '<END>': 2,\n",
    "    }\n",
    "    for line in token_list:\n",
    "        for token in line.split(' '):\n",
    "            if token not in token_dict:\n",
    "                token_dict[token]=len(token_dict)\n",
    "    return token_dict\n",
    "\n",
    "source_token_dict = build_token_dict(source_tokens)\n",
    "target_token_dict = build_token_dict(target_tokens)\n",
    "target_token_dict_inv = {v: k for k, v in target_token_dict.items()}\n",
    "\n",
    "# 添加特殊符号\n",
    "encode_tokens = [['<START>'] + tokens.split(' ') + ['<END>'] for tokens in source_tokens]\n",
    "decode_tokens = [['<START>'] + tokens.split(' ') + ['<END>'] for tokens in target_tokens]\n",
    "output_tokens = [tokens.split(' ') + ['<END>', '<PAD>'] for tokens in target_tokens]\n",
    "\n",
    "source_max_len = max(map(len, encode_tokens))\n",
    "target_max_len = max(map(len, decode_tokens))\n",
    "\n",
    "\n",
    "\n",
    "encode_tokens = [tokens + ['<PAD>'] * (source_max_len - len(tokens)) for tokens in encode_tokens]\n",
    "decode_tokens = [tokens + ['<PAD>'] * (target_max_len - len(tokens)) for tokens in decode_tokens]\n",
    "output_tokens = [tokens + ['<PAD>'] * (target_max_len - len(tokens)) for tokens in output_tokens]\n",
    "\n",
    "encode_input = [list(map(lambda x: source_token_dict[x], tokens)) for tokens in encode_tokens]\n",
    "decode_input = [list(map(lambda x: target_token_dict[x], tokens)) for tokens in decode_tokens]\n",
    "decode_output = [list(map(lambda x: [target_token_dict[x]], tokens)) for tokens in output_tokens]\n",
    "\n",
    "print(len(encode_input))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pickle\n",
    "import operator\n",
    "path = 'middle_data/'\n",
    "with open(path + 'encode_input.pkl', 'wb') as f:\n",
    "    pickle.dump(encode_input, f, pickle.HIGHEST_PROTOCOL)\n",
    "with open(path + 'decode_input.pkl', 'wb') as f:\n",
    "    pickle.dump(decode_input, f, pickle.HIGHEST_PROTOCOL)\n",
    "with open(path + 'decode_output.pkl', 'wb') as f:\n",
    "    pickle.dump(decode_output, f, pickle.HIGHEST_PROTOCOL)\n",
    "with open(path + 'source_token_dict.pkl', 'wb') as f:\n",
    "    pickle.dump(source_token_dict, f, pickle.HIGHEST_PROTOCOL)\n",
    "with open(path + 'target_token_dict.pkl', 'wb') as f:\n",
    "    pickle.dump(target_token_dict, f, pickle.HIGHEST_PROTOCOL)\n",
    "with open(path + 'source_tokens.pkl', 'wb') as f:\n",
    "    pickle.dump(source_tokens, f, pickle.HIGHEST_PROTOCOL)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
